<template>
	<view>
		<view class="date-picker">
			<uni-datetime-picker v-model="datetimerange" type="datetimerange" rangeSeparator="" />
			<button class="search-btn" type="primary" @click="searchData">查询</button>
		</view>

		<view class="table">
			<view class="boxItem radius-lg shadow-box bg-white  margin-tb-sm " v-for="item in dataList" :key="index">
				<view class="flex justify-between solid-bottom">
					<view class="flex justify-between width100 padding-tb-sm">
						{{ item.time }}
					</view>
				</view>
				<view class="detailItem">
					<template v-for="sitem in columns" :key="sindex">
						<text class="width50" v-if="columnsShow(sitem)">
							{{ sitem.label }}：{{ item[sitem.prop] }}
						</text>
					</template>
				</view>
			</view>
			<!-- <uni-loading v-if="loadingMore">加载中...</uni-loading> -->
			<uni-load-more :status="status"></uni-load-more>

		</view>
	</view>
</template>

<script>
	import {
		getDevHistoryData
	} from '../../api/index.js'
	import {
		mapState,
		mapActions
	} from 'vuex';
	export default {
		props: {
			areaId: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				start_date: '',
				end_date: '',
				dataList: [],
				columns: [],
				paramsForm: {
					page: 1,
					limit: 20,

				},
				datetimerange: this.getDates(),
				loadingMore: true,
				status: 'more',
				statusTypes: [{
					value: 'more',
					text: '加载前',
				}, {
					value: 'loading',
					text: '加载中',
				}, {
					value: 'noMore',
					text: '没有更多',
				}],
			};
		},
		computed: {
			...mapState(['areaObject'])
		},
		watch: {
			areaObject: {
				handler: function(val, oldVal) {
					this.bindArea()
				},
				// deep: true,
				immediate: true
			}
		},
		created() {
			// this.datetimerange = this.getDates()

		},
		methods: {

			// 获取当前时间和前三天的年月日时分秒
			getDates() {
				const now = new Date();
				const threeDaysAgo = new Date(now - (1 * 24 * 60 * 60 * 1000));
				let nowTime = this.formattedTime(
					now.getFullYear(),
					now.getMonth() + 1, // 月份是从0开始的
					now.getDate(),
					now.getHours(),
					now.getMinutes(),
					now.getSeconds())
				let beforeTime = this.formattedTime(
					threeDaysAgo.getFullYear(),
					threeDaysAgo.getMonth() + 1, // 月份是从0开始的
					threeDaysAgo.getDate(),
					threeDaysAgo.getHours(),
					threeDaysAgo.getMinutes(),
					threeDaysAgo.getSeconds())

				return [beforeTime, nowTime];

			},
			formattedTime(year, month, day, hours, minutes, seconds) {
				let nowStr = year + "-" +
					(month < 10 ? "0" : "") + month + "-" +
					(day < 10 ? "0" : "") + day + " " +
					(hours < 10 ? "0" : "") + hours + ":" +
					(minutes < 10 ? "0" : "") + minutes + ":" +
					(seconds < 10 ? "0" : "") + seconds;
				return nowStr
			},
			columnsShow(row) {
				let arr = ['time', 'shebeibianhao', 'address']
				if (arr.includes(row.prop)) {
					return false
				} else {
					return true
				}
			},
			bindArea() {
				if (this.areaObject.shebeibianhao) {
					this.getIrrigationHistory()
				}
			},
			changeLog(type) {
				if (type === 'start') {
					console.log('开始日期:', this.start_date);
				} else if (type === 'end') {
					console.log('结束日期:', this.end_date);
				}
			},
			searchData() {
				this.dataList = []
				this.paramsForm.page = 1
				this.loadingMore = true
				this.getIrrigationHistory();
			},
			// 获取灌溉历史数据
			getIrrigationHistory() {
				if (this.loadingMore) {
					this.status = 'loading'
					const data = {
						page: this.paramsForm.page,
						limit: this.paramsForm.limit,
						shebeibianhao: this.areaObject.shebeibianhao,
						startTime: this.datetimerange[0],
						endTime: this.datetimerange[1],
					};
					getDevHistoryData(data).then(res => {
						if (res.data.total > 0) {
							if (this.paramsForm.page == 1) {
								this.dataList = res.data.data.data;
								this.columns = res.data.data.th;
							} else {
								this.dataList = this.dataList.concat(res.data.data.data);
								if (res.data.data.totol == this.dataList.length) {
									this.loadingMore = false
									this.status = 'noMore'
								} else {
									this.status = 'more',
										this.loadingMore = true
								}
							}
						}
					});
				} else {
					uni.showToast({
						title: '没有更多数据',
						icon: 'none',
						duration: 2000
					});
					return
				}

			}
		}
	};
</script>

<style scoped lang="scss">
	/* Add any specific styles for your history component here */
	/* 数据表格样式 */
	.table {
		width: 100%;
		padding-top: 80px;

		.width50 {
			width: 50%;
		}
	}

	.boxItem {
		padding: 10px 15px;
		background: #fff;
		height: auto;
		margin: 10px 0px;
		border-radius: 5px;
		display: flex;
		flex-wrap: wrap;
		border: 1px solid #f1f1f1;

		.detailItem {
			display: flex;
			flex-wrap: wrap;
			width: 100%;
		}
	}

	.date-picker {
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: fixed;
		width: 97%;
		top: 135px;
		z-index: 99;
		background: #fff;
		height: 40px;

		::v-deep .uni-date__x-input {
			font-size: 24rpx;
		}

		.search-btn {
			height: 34px;
			line-height: 34px;
		}
	}

	.table-header {
		display: flex;
		background-color: #f5f5f5;
		padding: 10px 0;
	}

	.table-row {
		display: flex;
		background-color: #fff;

		padding: 10px 0;
		align-items: center;
	}

	.table-row:hover {
		background: rgba(0, 0, 0, 0.02);
	}

	.table-cell {
		flex: 1;
		text-align: center;
		font-size: 13px;
		font-weight: 300;
		flex-wrap: nowrap;
	}

	.table-row .table-cell:nth-last-child(-n + 2) {
		font-weight: 900;
	}

	.table-header .table-cell {
		font-weight: 400;
	}
</style>